var webpack = require("webpack"),
	path = require("path"),
	fileSystem = require("fs"),
	env = require("./utils/env"),
	HtmlWebpackPlugin = require("html-webpack-plugin"),
	CopyWebpackPlugin = require('copy-webpack-plugin'),
	WriteFilePlugin = require("write-file-webpack-plugin");

// load the secrets
var alias = {};

var secretsPath = path.join(__dirname, ("secrets." + env.NODE_ENV + ".js"));

var fileExtensions = ["jpg", "jpeg", "png", "gif", "eot", "otf", "svg", "ttf", "woff", "woff2"];

if (fileSystem.existsSync(secretsPath)) {
	alias["secrets"] = secretsPath;
}

var options = {
	entry: {
		popup: path.join(__dirname, "src", "js", "popup.js"),
		background: path.join(__dirname, "src", "js", "background.js"),
		content: path.join(__dirname, "src", "js", "content.js"),
		options: path.join(__dirname, "src", "js", "options.js"),
	},
	chromeExtensionBoilerplate: {
		notHotReload: ["content"]
	},
	output: {
		path:path.join(__dirname, env.TARGET_DIRNAME),
		filename: "[name].bundle.js"
	},
	module: {
		rules: [
			{
				test: /\.css$/,
				loader: "style-loader!css-loader",
				exclude: /node_modules/
			},
			{
				test: new RegExp('\.(' + fileExtensions.join('|') + ')$'),
				loader: "file-loader?name=[name].[ext]",
				exclude: /node_modules/
			},
			{
				test: /\.html$/,
				loader: "html-loader",
				exclude: /node_modules/
			},
			{
				test: /\.(js|jsx)$/,
				loader: "babel-loader",
				exclude: /node_modules/
			}
		]
	},
	resolve: {
		alias: alias,
		extensions: fileExtensions.map(extension => ("." + extension)).concat([".jsx", ".js", ".css"])
	},
	plugins: [
		// expose and write the allowed env vars on the compiled bundle
		new webpack.DefinePlugin({
			"process.env.NODE_ENV": JSON.stringify(env.NODE_ENV)
		}),
		new HtmlWebpackPlugin({
			template: path.join(__dirname, "src", "popup.html"),
			filename: "popup.html",
			chunks: ["popup"]
		}),
		new HtmlWebpackPlugin({
			template: path.join(__dirname, "src", "options.html"),
			filename: "options.html",
			chunks: ["options"]
		}),
		new HtmlWebpackPlugin({
			template: path.join(__dirname, "src", "background.html"),
			filename: "background.html",
			chunks: ["background"]
		}),
		new WriteFilePlugin(),
		new CopyWebpackPlugin([{from:"lib", to:'lib', toType:'dir'}], {debug:true,context:__dirname})
	]
};

if (env.NODE_ENV === "development") {
	//options.devtool = "cheap-module-eval-source-map";
	options.devtool = "source-map";
}

module.exports = options;
